<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>indexOf</title>
</head>

<body>
  <script>
    let a = [0, 1, 2, 1, 0]
    function findall(a, x) {
      let result = [],
        len = a.length,
        pos = 0;
      while (pos < len) {
        pos = a.indexOf(x, pos)
        if (pos == -1) break;
        result.push(pos);
        pos = pos + 1
      }
      return result
    }

    console.log(findall(a, 0));
  </script>
</body>

</html>